<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/1
 * Time: 下午9:05
 */
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class ConfigController extends AdminBaseController
{
    public function index()
    {
        $config = M('Config');
        $list = $config  -> select();
        $this->assign('list', $list);
        $this->display();
    }

    public function addSave()
    {
        if(IS_POST || IS_POST){
            $config = M('Config');
            $configInfo = $config -> select();

            $sql = 'UPDATE dx_config SET `value` = CASE `variable`';
            $variables = '';
            foreach($configInfo as $value){
                $variable = $value['variable'];
                $$variable = I('post.' . $variable . '/s','');

                if(!$$variable){
                    $this -> ajaxReturn(['statsu' => 1 , 'msg' => $value['title'] . '不能为空！']);
                }

                $sql .= ' WHEN "' . $variable . '" THEN "' . $$variable . '"';
                $variables .= '"' . $variable . '"' . ',';

            }
            $variables = trim($variables, ',');
            $sql .= 'END WHERE `variable` IN (' . $variables . ')';

            $isUp = $config -> execute($sql);
            if(false === $isUp){
                $this -> ajaxReturn(['status' => 1, 'msg' => '更新失败']);
            }elseif(0 === $isUp){
                $this -> ajaxReturn(['status' => 1, 'msg' => '别闹...你没有更新任何内容']);
            }
            $this -> ajaxReturn(['status' => 0, 'msg' => '更新成功']);
        }
   }
}